﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Configuration;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cForgotPassword : System.Web.UI.UserControl
    {
        AccountManagement accountMana = new AccountManagement();
        CompanyBussiness.MD5 md5 = new CompanyBussiness.MD5();
        CompanyBussiness.Account account = new Account();
        CheckValid cv = new CheckValid();
        int check;
        private string ps = "";
        SendEmail sendEmail=new SendEmail();

        protected void Page_Load(object sender, EventArgs e)
        {
            fail.Visible = false;
        }

        protected void btnResetPass_Click(object sender, EventArgs e)
        {
            try
            {
                CaptchaControl1.ValidateCaptcha(txtCapcha.Text.Trim());
                if (txtUserName.Text != "" && txtEmail.Text != "")
                {
                    if (CaptchaControl1.UserValidated)
                    {
                        if (cv.CheckEmail(txtEmail.Text))
                        {
                            if (accountMana.ForgotPassword(txtUserName.Text, txtEmail.Text) != null)
                            {
                                byte[] pass = new byte[6];
                                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                rng.GetBytes(pass);
                                ps = Convert.ToBase64String(pass);

                                account = accountMana.GetAccountByUserName(txtUserName.Text);
                                accountMana.UpdateAccount(int.Parse(account.AccountID.ToString()), account.UserName.ToString(), md5.EnMD5(ps.ToString()), account.FirstName.ToString(), account.LastName.ToString(), account.Email.ToString(), account.Nick.ToString(), account.Address.ToString(), account.Phone.ToString(), Boolean.Parse(account.Gender.ToString()), account.Images.ToString(), Boolean.Parse(account.Roles.ToString()));

                                sendEmail.SendMail(account.Email.ToString(), ps);

                                Response.Redirect("Login.aspx");
                            }
                            else
                            {
                                fail.Visible = true;
                                lbError.Text = "UserName hoặc email không đúng!";
                            }
                        }
                        else
                        {
                            fail.Visible = true;
                            lbError.Text = "Email không đúng định dạng !";
                        }                                                    
                    }
                    else
                    {
                        fail.Visible = true;
                        lbError.Text = "Hãy nhập mã trong hình và thử lại...";
                    }                  
                }
                else
                {
                    fail.Visible = true;
                    lbError.Text = "Bạn phải nhập đầy đủ thông tin !";
                    return;
                }
            }
            catch (Exception)
            {
                fail.Visible = true;
                lbError.Text = "Có lỗi Xin vui lòng thử lại !";
                return;
            }            
        }
    }
}